package com.ying.rabbitmqspringbootconsumer.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * ClassName: OrderDLQListener
 * Package: com.ying.rabbitmqspringbootconsumer.listener
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/7/26 20:59
 * @Version 1.0
 */

@Component
public class OrderDLQListener {

    @RabbitListener(queues = "orderDLQueue")
    public void orderDLQListener(Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            System.out.println(new String(message.getBody()));

            System.out.println("业务处理");

            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {

            System.out.println("出现异常");

            try {
                channel.basicNack(deliveryTag,true,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

        }
    }
}
